package com.hezhi.framework.util;

import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.io.*;

/**
 * 30june2006 this project has to goto the Production and today [28/june/2006]
 * we came to know and need to solve this issue of pwd encrypt
 * 
 * This program uses the one way hashing with the help of message digest
 * algorithms. The most common two message digest algorithms, and the ones that
 * are provided with the SunJCE provider are: MD5 [128-bit digest ] Secure Hash
 * Standard (SHA-1) [160-bit digest ] Given Pwd : Sc00ter
 * AkJphNwgz6p3XrweNIKK0A== C2+jJwuDjOUQRLp7bS3amRCzAWA=
 */

public final class EncryptUtil {

	private static EncryptUtil instance;

	private EncryptUtil() {
	}

	public static synchronized EncryptUtil getInstance() {
		if (instance == null) {
			instance = new EncryptUtil();
		}
		return instance;
	}

	/*
	 * public static void main(String h[]){ String pwd = "Sc00ter";
	 * 
	 * System.out.println("Given Pwd : " + pwd);
	 * System.out.println(encryptMD5(pwd)); System.out.println(encryptSHA(pwd));
	 * }
	 */

	// First try
	public static synchronized String encryptMD5(String strEnc)// throws
	{
		MessageDigest md = null;
		try {
			// md = MessageDigest.getInstance("SHA");
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException nsaex) {
			// throw new
			nsaex.printStackTrace();
		}
		try {
			md.update(strEnc.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException useex) {
			// throw new
			useex.printStackTrace();
		}

		byte raw[] = md.digest();
		String hash = (new BASE64Encoder()).encode(raw);
		return hash;
		// return getString(raw);
	}

	public synchronized String encryptSHA(String strEnc)// throws
	{
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA");
			// md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException nsaex) {
			nsaex.printStackTrace();
		}
		try {
			md.update(strEnc.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException useex) {
			useex.printStackTrace();
		}

		byte raw[] = md.digest();
		String hash = (new BASE64Encoder()).encode(raw);
		return hash;
	}

	// We can use either the above [First try] or the below [Second try] set of
	// funtions/methods.
	// Second try
	private static String getString(byte[] bytes) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			byte b = bytes[i];
			sb.append((int) (0x00FF & b));
			if (i + 1 < bytes.length) {
				sb.append("-");
			}
		}
		return sb.toString();
	}

	@SuppressWarnings("unused")
	private static byte[] getBytes(String str) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		StringTokenizer st = new StringTokenizer(str, "-", false);
		while (st.hasMoreTokens()) {
			int i = Integer.parseInt(st.nextToken());
			bos.write((byte) i);
		}
		return bos.toByteArray();
	}

	public static String md5(String source) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] bytes = md.digest(source.getBytes());
			return getString(bytes);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String sha(String source) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA");
			byte[] bytes = md.digest(source.getBytes());
			return getString(bytes);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
